package com.grao.waterschool.human.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.grao.waterschool.human.common.R;
import com.grao.waterschool.human.entiry.dto.CourseHistoryDto;
import com.grao.waterschool.human.entiry.dto.GetCourseHistory;
import com.grao.waterschool.human.entiry.vo.AvgInfo;
import com.grao.waterschool.human.entiry.vo.CourseHistoryVO;
import com.grao.waterschool.human.entiry.vo.HistoryUpdateInfo;
import com.grao.waterschool.human.manager.service.CourseHistoryListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Tag(name = "课程历史查询接口")
@RestController
@RequestMapping("/courseHistoryList/")
@Slf4j
public class CourseHistoryListController {

    @Autowired
    private CourseHistoryListService courseHistoryListService;

/*    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "插入课程历史")
    @Parameter(name = "insertCourseHistory")
    @PostMapping("insert")
    public R<String> insertCourseHistory(@RequestBody CourseHistoryDto courseHistoryDto) {
        R<String> result = courseHistoryListService.insertCourseHistory(courseHistoryDto);
        return result.getStatus() == 0 ? R.success(result.getMsg()) : R.error(result.getMsg());
    }*/


    @Parameter(name = "collegeList", description = "将需要查看平均数值的院系部名字作为参数传入")
    @Operation(summary = "根据院系名字查看平均数值")
    @PostMapping("avg")
    public R<List<AvgInfo>> selectCourseHistory(@RequestBody CourseHistoryDto courseHistoryDto) {
        List<AvgInfo> avgBycollege = courseHistoryListService.getAvgBycollege(courseHistoryDto);

        return R.success(avgBycollege);
    }

/*    @Operation(summary = "删除课程历史")
    @Parameter(name = "courseHistoryId", description = "courseHistoryId")
    @DeleteMapping("delete")
    public R<String> deleteCourseHistory(@RequestParam("courseHistoryId") String courseHistoryId) {
        courseHistoryListService.deleteCourseHistory(courseHistoryId);
        return result.getStatus() == 0 ? R.success(result.getMsg()) : R.error(result.getMsg());
    }*/

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "课程历史查询")
    @Parameter(name = "getCourseHistory", description = "ZC、JSH、JGH、")
    @PostMapping("select")
    public R<List<Object>> getCourseHistoryList(@RequestBody GetCourseHistory getCourseHistory) {
        R<List<CourseHistoryVO>> courseHistoryList = courseHistoryListService.getCourseHistoryList(getCourseHistory);
        return courseHistoryList.getStatus() == 0 ? R.success(courseHistoryList.getMsg(), Collections.singletonList(courseHistoryList.getData())) : R.error(courseHistoryList.getMsg());
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "课程历史导出")
    @Parameter(name = "getCourseHistory", description = "ZC、JSH、JGH")
    @PostMapping("output")
    public void outPutCourseHistoryList(@RequestBody List<GetCourseHistory> getCourseHistory, HttpServletResponse response) throws IOException {
        log.info("getCourseHistoryList: {}", getCourseHistory);
        courseHistoryListService.outPutCourseHistory(getCourseHistory, response);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "根据图片名字下载图片")
    @Parameter(name = "picName", description = "picName")
    @PostMapping("getPicByName")
    public void getPicByName(@RequestParam("picName") String picName, HttpServletResponse response) throws Exception {

        courseHistoryListService.getPicObject(picName, response);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "更新课程历史")
    @Parameter(name = "historyUpdateInfo", description = "historyUpdateInfo")
    @PostMapping("update")
    public R<String> updateCourseHistory(@RequestBody HistoryUpdateInfo historyUpdateInfo) {
        R<String> result = courseHistoryListService.updateCourseHistory(historyUpdateInfo);
        return result.getStatus() == 0 ? R.success(result.getMsg()) : R.error(result.getMsg());
    }

    @Parameter(name = "collegeList", description = "查询手动巡检记录")
    @Operation(summary = "查询手动巡检记录")
    @PostMapping("list")
    public R<List<Object>> getCourseHistoryByType(@RequestBody CourseHistoryDto courseHistoryDto) {
        R<List<CourseHistoryVO>> courseHistoryList = courseHistoryListService.getCourseHistoryByType(courseHistoryDto);

        return  courseHistoryList.getStatus() == 0 ? R.success(courseHistoryList.getMsg(), Collections.singletonList(courseHistoryList.getData())) : R.error(courseHistoryList.getMsg());
    }

    @Parameter(name = "courseHistoryDetail", description = "查询巡检详情")
    @Operation(summary = "查询巡检详情")
    @PostMapping("courseHistoryDetail")
    public R<List<Object>> courseHistoryDetail(@RequestParam("kch") String kch) {
        R<List<CourseHistoryVO>> courseHistoryList = courseHistoryListService.courseHistoryDetail(kch);

        return  courseHistoryList.getStatus() == 0 ? R.success(courseHistoryList.getMsg(), Collections.singletonList(courseHistoryList.getData())) : R.error(courseHistoryList.getMsg());
    }

    @Parameter(name = "deleteById", description = "查询巡检详情")
    @Operation(summary = "删除手动巡课记录")
    @PostMapping("deleteById")
    public R deleteById(@RequestParam("id") String id) {
        courseHistoryListService.deleteById(id);
        return R.success();
    }
}
